<?php
namespace apps\marriage\app\command;

use EasyTask\Helper;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Env;
use vm\org\queue\QueueFactory;

/**
 * Class Task
 * @package apps\marriage\app\command
 * 启动：  /usr/local/php/bin/php -c /usr/local/php/etc/php.ini ./think task_marriage start --daemon
 * 停止： /usr/local/php/bin/php -c /usr/local/php/etc/php.ini ./think task_marriage stop
 * 强制停止： /usr/local/php/bin/php -c /usr/local/php/etc/php.ini ./think task_marriage stop --force
 */
class Task extends Command
{
    private $logFile;

    public function __construct()
    {
        parent::__construct();
        $this->logFile = DOC_PATH . 'runtime/log/daemon/marriage_'.date('Ymd').'.txt';
    }

    protected function configure()
    {
        //设置名称为task
        $this->setName('task_marriage')
            //增加一个命令参数
            ->addArgument('action', Argument::OPTIONAL, "action", '')
            ->addOption('force', 'f', Option::VALUE_NONE, '强制退出')
            ->addOption('daemon', 'd', Option::VALUE_NONE, '设置为守护进程');
    }

    protected function execute(Input $input, Output $output)
    {
        //获取输入参数
        $action = trim($input->getArgument('action'));
        $force = $input->getOption('force') ? true : false;
        $daemon = $input->getOption('daemon') ? true : false;

        // 配置任务
        $task = new \EasyTask\Task();
        $task->setPrefix('task_marriage');
        $task->setTimeZone('Asia/Shanghai');
        $task->setDaemon($daemon);
        $task->setAutoRecover(true);
        $runtimePath = app()->getRuntimePath() . 'task' . DIRECTORY_SEPARATOR . 'marriage' . DIRECTORY_SEPARATOR;
        if(!is_dir($runtimePath)) {
            makeDir($runtimePath);
        }
        $task->setRunTimePath($runtimePath);

        $timerInterval = Helper::canUseEvent() ? 1 : 1;

        $logFile = $this->logFile;

        $task->addFunc(function () use ($logFile) {
            static $count = 1;

            writeLog('发送公众号通知 BEGIN count:' . $count, $logFile);

            static $config = null, $queueHandler = null, $weixinMpLogic = null;
            try {
                if(is_null($config)) {
                    $config = config('queue');
                }
                if(is_null($queueHandler)) {
//                    $queueHandler = QueueFactory::instance('Redis', [
//                        'host' => $config['host'],
//                        'port' => $config['port'],
//                        'password' => $config['password'],
//                        'prefix' => $config['prefix'],
//                    ]);
                    $queueHandler = QueueFactory::instance();
                }
                if(is_null($weixinMpLogic)) {
                    $weixinMpLogic = logic('WeixinMp', LOGIC_NAMESPACE);
                }

                for($k = 1; $k <= 10; $k++) {
                    $msg = $queueHandler->get($config['queues']['mp_msg']);
                    if($msg) {
                        $weixinMpLogic->sendTemplateMessage($msg['openid'], $msg['template_id'], $msg['data'], $msg['url'], $msg['miniprogram']);
                        usleep(200);
                    }
                }
            } catch (\Exception $e) {
                writeLog('发送公众号通知 出现错误,ERROR:', $logFile);
                writeLog($e->getFile() . "\r\n" . $e->getLine() . "\r\n" . $e->getMessage(), $logFile);
            }
            writeLog('发送公众号通知 END count:' . $count, $logFile);

            if($count == 600) {
                exit(0);
            }
            $count++;
        }, 'push_mp_msg', $timerInterval, 1);

        $task->addFunc(function () use ($logFile) {
            static $count = 1;

            writeLog('刷新用户vip等级 BEGIN count:' . $count, $logFile);

            static $memberProfileService = null;
            try {
                // 将管理员发布的通知推到用户通知表
                if(is_null($memberProfileService)) {
                    $memberProfileService = service('MemberProfile', SERVICE_NAMESPACE);
                }
                $memberProfileService->onRefreshUserVipLevel();
            } catch (\Exception $e) {
                writeLog('刷新用户vip等级 出现错误,ERROR:', $logFile);
                writeLog($e->getFile() . "\r\n" . $e->getLine() . "\r\n" . $e->getMessage(), $logFile);
            }

            writeLog('刷新用户vip等级 END count:' . $count, $logFile);

            if($count == 600) {
                exit(0);
            }
            $count++;
        }, 'refresh_user_vip_level', $timerInterval, 1);

        // 根据命令执行
        if ($action == 'start') {
            $task->start();
        } elseif ($action == 'status') {
            $task->status();
        } elseif ($action == 'stop') {
            //是否强制停止
            $task->stop($force);
        } else {
            exit('Command is not exist');
        }
    }
}